<?php

class JpushSetModel
{
    private $jpushKeyPrefix   =   'JP/';
    private $diaryListCacheModel;
    
    public function __construct(){
        $this->diaryListCacheModel  =   new DiaryListCacheModel();
    }
    
    public function addJpush($info)/*{{{*/
    {
    	$data = array();
    	isset($info['master_control']) 		&&	$data['master_control'] 	= $info['master_control'];
    	isset($info['dnd_set']) 		&&	$data['dnd_set'] 		= $info['dnd_set'];
    	isset($info['commentPush']) 		&&	$data['commentPush'] 		= $info['commentPush'];
    	isset($info['messagePush']) 		&&	$data['messagePush'] 		= $info['messagePush'];
    	isset($info['followPush']) 		&&	$data['followPush'] 		= $info['followPush'];
    	isset($info['content_show']) 		&&	$data['content_show'] 		= $info['content_show'];
    	isset($info['sound_show']) 		&&	$data['sound_show'] 		= $info['sound_show'];
    	isset($info['jpush_extra']) 		&&	$data['jpush_extra'] 		= $info['jpush_extra'];
    	if (isset($data['dnd_set']) && $data['dnd_set'] == 0)
    	{
	    	// 夜间免打扰模式23:00-8:00
	    	$data['startHour'] = AC::START_HOUR;
	    	$data['endHour']   = AC::END_HOUR;
	    	isset($info['startHour']) 			&&	$data['startHour'] 			= $info['startHour'];
	    	isset($info['endHour']) 			&&	$data['endHour'] 			= $info['endHour'];
	    	if ($data['startHour'] == $data['endHour'] || $data['startHour'] < 0 || $data['startHour'] > 24)
	    	{
	    		AC::$errorNo  = 'JPUSH005';
	    		return FALSE;
	    	}
    	}
    	if ($data['jpush_extra']) {
    	    $data['jpush_extra'] = $this->filterTagSetting($data['jpush_extra']);
    	}
    	$data['uid']		=	$info['uid'];
    	$data['deviceSN'] 	= 	$info['deviceSN'];
    	$data['time']		=	AC::SERVER_TIME;
    	
    	$key    =   $this->jpushKeyPrefix. $data['uid'];
    	$result =   Storage::addData($key, $data);
    	return  true;
    }/*}}}*/
    
    /**
     * 推送总开关
     * @param $info
     * @return boolean
     */
    public function setStop($info)/*{{{*/
    {
   		if (empty($info['uid']))
    	{
    		AC::$errorNo    =   'JPUSH004';
    		return  FALSE;
    	}
    	
    	$data = array();
    	$data['time']			=	AC::SERVER_TIME;
    	isset($info['master_control']) 	&&	$data['master_control'] = $info['master_control'];
    	
    	$key    =   $this->jpushKeyPrefix. $info['uid'];
    	$where  =   array('uid' => $info['uid']);
    	$result =   Storage::setConditionData($key, $data, $where);
   	 	if (!$result)
    	{
    		AC::$errorNo  = 'JPUSH008';
    		return FALSE;
    	}
    	
    	$this->diaryListCacheModel->delUserPushInfoCache($info['uid']);
    	return  true;
    }
    
    /**
     * 模块推送
     * @param unknown $info
     * @return boolean|Ambigous <boolean, unknown>
     */
    public function setPushTag($info)/*{{{*/
    {	
    	if (empty($info['uid']))
    	{
    		AC::$errorNo    =   'JPUSH004';
    		return  FALSE;
    	}
    	
    	$data = array();
    	isset($info['messagePush']) 	&&	$data['messagePush'] 	= $info['messagePush'];
    	isset($info['followPush']) 	&&	$data['followPush'] 	= $info['followPush'];
    	isset($info['commentPush'])     &&	$data['commentPush'] 	= $info['commentPush'];
    		
    	$data['time']	=	AC::SERVER_TIME;
    	
    	$key    =   $this->jpushKeyPrefix. $info['uid'];
    	$where  =   array('uid' => $info['uid']);
    	$result =   Storage::setConditionData($key, $data, $where);
    	if (!$result)
    	{
    		AC::$errorNo  = 'JPUSH007';
    		return FALSE;
    	}
    	
    	$this->diaryListCacheModel->delUserPushInfoCache($info['uid']);
    	return  true;
    }
    
    public function setPush($info)/*{{{*/
    {
    	if (empty($info['uid']))
    	{
    		AC::$errorNo    =   'JPUSH001';
    		return  FALSE;
    	}
    	
    	$data = array();
    	isset($info['deviceSN']) 		&&	$data['deviceSN'] 		= $info['deviceSN'];
    	isset($info['sound_show']) 		&&	$data['sound_show'] 		= $info['sound_show'];
    	isset($info['content_show']) 		&&	$data['content_show'] 		= $info['content_show'];
    	isset($info['dnd_set']) 		&&	$data['dnd_set'] 		= $info['dnd_set'];
    	isset($info['jpush_extra']) 		&&	$data['jpush_extra'] 		= $info['jpush_extra'];
    	if (isset($data['dnd_set']) && $data['dnd_set'] != 1 )
    	{	
	    	// 夜间免打扰模式23:00-8:00
	    	$data['startHour'] = AC::START_HOUR;
	    	$data['endHour']   = AC::END_HOUR;
	    	isset($info['startHour']) 			&&	$data['startHour'] 			= $info['startHour'];
	    	isset($info['endHour']) 			&&	$data['endHour'] 			= $info['endHour'];
	    	if ($data['startHour'] == $data['endHour'] || $data['startHour'] < 0 || $data['endHour'] > 24)
	    	{
	    		AC::$errorNo  = 'JPUSH005';
	    		return FALSE;
	    	}
    	}
    	if ($data['jpush_extra']) {
    	    $data['jpush_extra'] = $this->filterTagSetting($data['jpush_extra']);
    	}
    	$data['time']	=	AC::SERVER_TIME;
    	
        $key    =   $this->jpushKeyPrefix. $info['uid'];
        $where  =   array('uid' => $info['uid']);
        $result =   Storage::setConditionData($key, $data, $where);
    	if (!$result)
    	{
    		AC::$errorNo  = 'JPUSH006';
    		return FALSE;
    	}
    	
    	$this->diaryListCacheModel->delUserPushInfoCache($info['uid']);
        return  true;
    }/*}}}*/
    
    # 获取用户的推送设置
    public function getPushInfo($info){
    	if (empty($info['uid'])){
    		AC::$errorNo    =   'JPUSH001';
    		return  FALSE;
    	}
    	
    	$result = $this->diaryListCacheModel->getUserPushInfoCache($info['uid']);
    	if (empty($result)) {
    	    $key    =   $this->jpushKeyPrefix. $info['uid'];
    	    $where  =   array('uid' => $info['uid']);
    	    $result =   Storage::getConditionData($key, $where);
            if (!empty($result)) {
                $this->diaryListCacheModel->setUserPushInfoCache($info['uid'], $result);
            }
    	}
    	
    	if ($result['jpush_extra']) {
    	    $result['jpush_extra'] = $this->filterTagSetting($result['jpush_extra']);
    	}
    	
    	return  $result;
    }
    
    private function filterTagSetting ($extra) {
        if (UK::isJSON($extra)) {
            $extra_arr = json_decode($extra, true);
            $extra_arr['tags'] = $this->rebuildTagsData($extra_arr['tags']);
            return json_encode($extra_arr);
        }
        
        $extra['tags'] = $this->rebuildTagsData($extra['tags']);
        return $extra;
    }
    
    private function rebuildTagsData ($tags) {
        if (empty($tags)) return array();
        
        $tags_new = array();
        foreach ($tags as $value) {
            if (strpos('a'.$value, 'chatgroup_') == 1){
                continue;
            }
            $tags_new[] = $value;
        }
        return $tags_new;
    }
}